//
//  NWOverlayView.m
//  noworry
//
//  Created by dl on 2017/10/21.
//  Copyright © 2017年 dl. All rights reserved.
//

#import "NWOverlayView.h"
#import <YYText/YYText.h>
#import "UIColor+NWHex.h"
#import "UIView+NWViewFrame.h"
#import <BlocksKit/BlocksKit.h>

@implementation NWOverlayView {
    UIImageView *_imgView;
    YYLabel *_titleLabel;
    YYLabel *_reloadTextLabel;
    UIActivityIndicatorView *_activityView;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _overlayStatus = NWOverlayStatusRemove;
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reloadTableViewDataSource)]];
        
        [self loadView];
        [self reloadView];
    }
    return self;
}

- (void) loadView {
    _imgView = [[UIImageView alloc] init];
    [self addSubview:_imgView];
    
    _titleLabel = [[YYLabel alloc] init];
    _titleLabel.textColor = [UIColor nw_colorWithHex:0x555555];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
    
    _reloadTextLabel = [[YYLabel alloc] init];
    _reloadTextLabel.textColor = [UIColor nw_colorWithHex:0xFF770E];
    _reloadTextLabel.textAlignment = NSTextAlignmentCenter;
    _reloadTextLabel.text = @"刷新页面";
    [self addSubview:_reloadTextLabel];
    
    _activityView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.f, 20.f)];
    [_activityView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self addSubview:_activityView];
}

- (void) layoutSubviews {
    [super layoutSubviews];
    
    _imgView.top = self.height * .3f;
    _imgView.size = _imgView.image.size;
    _imgView.centerX = self.centerX;
    
    _titleLabel.size = CGSizeMake(self.width, 24.f);
    _titleLabel.top = _imgView.bottom + 9.f;
    _titleLabel.centerX = self.centerX;
    
    _reloadTextLabel.size = CGSizeMake(self.width, 24.f);
    _reloadTextLabel.top = _titleLabel.bottom + 20.f;
    _reloadTextLabel.centerX = self.centerX;
    
    _activityView.size = CGSizeMake(20.f, 20.f);
    _activityView.center = self.center;
}

- (void) setOverlayStatus:(NWOverlayStatus)overlayStatus {
    _overlayStatus = overlayStatus;
    
    [self reloadView];
}

- (void) reloadView {
    self.alpha = self.overlayStatus == NWOverlayStatusRemove ? 0.f : 1.f;
    
    NSDictionary *statusMap = @{
        @"img": @[@(NWOverlayStatusLoading)],
        @"title": @[@(NWOverlayStatusLoading)],
        @"reload": @[@(NWOverlayStatusEmpty), @(NWOverlayStatusLoading)],
        @"activity": @[@(NWOverlayStatusLoading)]
    };
    
    [statusMap bk_each:^(NSString *key, NSArray *status) {
        BOOL match = [status containsObject:@(self.overlayStatus)];
        if ([key isEqualToString:@"img"]) {
            _imgView.hidden = match;
            _imgView.image = [UIImage imageNamed:@"icon_overlay_empty"];
        }
        
        if ([key isEqualToString:@"title"]) {
            _titleLabel.hidden = match;
            if (self.overlayStatus == NWOverlayStatusEmpty) {
                _titleLabel.text = @"暂无数据";
            } else if (self.overlayStatus == NWOverlayStatusError) {
                _titleLabel.text = @"服务器开了小差儿，请稍后再试";
            } else {
                _titleLabel.text = @"出错啦，请稍后再试";
            }
        }
        
        if ([key isEqualToString:@"reload"]) {
            _reloadTextLabel.hidden = match;
        }
        
        if ([key isEqualToString:@"activity"]) {
            match ? [_activityView startAnimating] : [_activityView stopAnimating];
        }
    }];
    [self setNeedsLayout];
}

#pragma mark - event
- (void) reloadTableViewDataSource {
    if ([@[@(NWOverlayStatusEmpty), @(NWOverlayStatusError), @(NWOverlayStatusReload)] containsObject:@(self.overlayStatus)]) {
        !self.reloadBlockIfErrOrReload ?: self.reloadBlockIfErrOrReload(self);
    }
}

@end
